const app = getApp()
const globalUrl = app.globalData.globalUrl
const img = app.globalData.img
var check = app.globalData.check

Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    money: 0,
    check: check,
    inputBoxAnimation: {},
    inputAnimation: {},
    hide: {},
    isInputBox: false,
    isInputed: true,
    img: img
  },
  goRecord(){
    wx.navigateTo({
      url: '../record/record',
    })
  },

  // 隐藏余额导入
  hideInputBox(){
    // 隐藏导入块
    var animation1 = wx.createAnimation({
      duration: 800,
      timingFunction: 'ease',
    });
    animation1.translateX(400).opacity(0).step()
    // 隐藏输入框
    var animation3 = wx.createAnimation({
      duration: 800,
      timingFunction: 'linear',
    });
    animation3.translateX(400).opacity(0).step()
    //显示充值块
    var animation2 = wx.createAnimation({
      duration: 800,
      timingFunction: 'ease',
    });
    animation2.translateX(0).opacity(1).step()
    this.setData({
      inputBoxAnimation: animation1.export(),
      inputAnimation: animation3.export(),
      hide: animation2.export(),
      checkHidden: false
    })
  },
  // 显示余额导入
  showInputBox(){
      this.setData({ isInputBox: true })
      // 显示导入块
      var animation1 = wx.createAnimation({
        duration: 800,
        timingFunction: 'ease',
      });
      animation1.translateY(-95).translateX(0).opacity(1).step()
      // 显示输入框
      var animation3 = wx.createAnimation({
        duration: 800,
        timingFunction: 'linear',
      });
      animation3.translateX(0).opacity(1).step()
      //隐藏充值块
      var animation2 = wx.createAnimation({
        duration: 800,
        timingFunction: 'ease',
      });
      animation2.translateX(-400).opacity(0).step()
    

      this.setData({
        inputBoxAnimation: animation1.export(),
        inputAnimation: animation3.export(),
        hide: animation2.export(),
        checkHidden: true
      })
  },
  //余额充值 
  Recharge(e){
    // console.log(e.currentTarget.dataset.money)
    if (!this.data.check) {
      wx.showToast({
        title: '请先同意协议',
        image: '/dingdang/images/icon/warn.png'
      })
      return
    }
    wx.showLoading({
      title: '请求中...',
      mask: true
    })
    var money = e.currentTarget.dataset.money
    wx.request({
      url: globalUrl + 'passenger/recharge',
      header: app.globalData.header,
      method: 'POST',
      data: {
        "is_agree_agreement": true,
        "price": money * 100
      },
      success:res =>{
        wx.hideLoading()
        if (res.data.serviceResult == 1101){
          wx.showToast({
            title: '暂时无法充值',
            image: '/dingdang/images/icon/warn.png'
          })
          return
        }
        var data = res.data.resultParm
        wx.requestPayment({
          'timeStamp': data.timeStamp,
          'nonceStr': data.nonceStr,
          'package': data.package,
          'signType': 'MD5',
          'paySign': data.paySign,
          'success': res =>{
            this.getCharge()
            wx.showToast({
              title: '充值成功！'
            })
          },
          'fail':function(res){
            wx.showToast({
              title: '支付失败',
              image: '/dingdang/images/icon/warn.png'
            }) 
          },
          'complete':function(res){}
        })
        
      }
    })
  },
  //同意协议
  check(){
    this.setData({
      check: !this.data.check
    })
    app.globalData.check = this.data.check
  },
  onShow(){
    this.showInputBox()
    this.hideInputBox()
    this.setData({ check: app.globalData.check })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#3aadfe',
      animation: {
        duration: 0,
        timingFunc: 'linear'
      }
    })
    wx.hideLoading()
    this.getCharge()
    const animation = wx.createAnimation({
      duration: 800,
      timingFunction: 'ease',
    });
    // this.animation = animation
    animation.translateX(400).opacity(0).step()
    
    this.setData({
      inputAnimation: {},
      inputBoxAnimation: animation.export(),
      hide: {}
    })
    wx.request({
      url: globalUrl + 'passenger/isImport',
      header: app.globalData.header,
      method: 'GET',
      success: res => {
        console.log(res)
        if (res.data.resultParm && !res.data.resultParm.isImport){
          this.setData({
            isInputed: false
          })
        }
      }
    })
  },
  //查询余额请求
  getCharge(){
    wx.request({
      url: globalUrl + 'passenger/getBalance',
      header: app.globalData.header,
      method: 'GET',
      success: res => {
        console.log(res)
        if(res.data.resultParm)
          this.setData({
            money: res.data.resultParm.money /100
          })
      }
    })
  },
  showProto(){
    var that = this
    wx.showModal({
      confirmText: '同意',
      title: '充值说明',
      content: '      第⼀条 叮当布袋程序（以下简称“叮当布袋”）由湛江市北极狐信息⽹络科技公司开发设置的小程序服务程序，⽤户可利⽤⾃⼰的微信账号绑定叮当布袋账户，利⽤叮当布袋平台消费搭乘环校⻋服务。\r\n\r\n      第⼆条 ⽤户充值后，账户余额不可提现、不可退款、不可透⽀、不计利息，账户余额仅限⽤户个⼈使⽤，不能转移、转赠。账户余额请您根据⾃⼰的消费情况充值，叮当布袋对充值次数不设限制。\r\n\r\n      第三条 ⽤户办理最低充值⾦额不得低于 20 元，后续充值为三个档次的定额充值，分别为：20 元、30 元、50 元（具体详⻅⼩程序充值界⾯）。\r\n\r\n      第四条 叮当布袋账户开通后，申请⼈应当妥善保管账号、密码及相关信息，叮当布袋平台不对因申请⼈原因导致的包括不限于账号、密码及相关信息的丢失、泄露、被窃或未经授权使⽤负责。\r\n\r\n      第五条 叮当布袋账户应由账户所有⼈使⽤和保管。账户持有⼈因向他⼈转借、转让或因保管不慎⽽发⽣的⼀切消费费⽤及损失由账户持有⼈承担。\r\n\r\n      第六条 本公司及其关联公司不提供与叮当布袋账户有关的明示或暗示的保证，包括但不限于对于其商业性或适⽤于特定⽤途的任何明示或暗示的保证。\r\n\r\n      第七条 有关叮当布袋账户相关解释均适⽤中华⼈⺠共和国法律。\r\n\r\n      第⼋条 ⽤户对充值说明内容清楚，⾃愿接受充值说明的约束。',
      success(res){
        if (res.confirm) {
          that.setData({
            check: true
          })
        }
      }
    })
  },
  formSubmit(e) {
    var account = e.detail.value.account
    var name = e.detail.value.name
    if (account == '' | name == '') {
      wx.showToast({
        title: '请先填好信息',
        image: '/dingdang/images/icon/warn.png'
      })
      return
    }
    //余额导入
    wx.showLoading({
      mask: true
    })
    wx.request({
      url: globalUrl + 'passenger/card',
      header: app.globalData.header,
      method: 'POST',
      data: {
        "account": account,
        "name": name
      },
      success: res => {
        wx.hideLoading()
        var result = res.data.serviceResult
        if (result == 1) {
          wx.showToast({
            title: '导入成功',
          })
          this.getCharge()
          this.hideInputBox()
          this.setData({
            isInputed: true 
          })
        } else
          if (result == 2) {
            wx.showToast({
              title: res.data.resultInfo,
              icon: "none"
            })
          } else
            if (result == 113) {
              wx.showToast({
                title: "余额已经转入",
                icon: "none"
              })
            }
            else
              if (result == 114) {
                wx.showToast({
                  title: "账号姓名错误",
                  icon: "none"
                })
              }
      }
    })
  },
})